package com.yaolisheng.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2021/5/5 12:16
 */
public final class DateUtils {
	
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT_SHORT = "yyyyMMdd";
	public static final String DATE_TIME_FORMAT_SHORT = "yyyyMMddHHmmss";
	public static final String TIME_FORMAT = "HH:mm:ss";
	public static final String TIME_FORMAT_SHORT = "HHmmss";
	
	/**
	 * 取年份
	 * @param date
	 * @return
	 */
	public static int getYear(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}
	
	/**
	 * 取月份
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * 取日期
	 * @param date
	 * @return
	 */
	public static int getDay(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

    /**
     * 取季度
     * @param date
     * @return
     */
    public static int getQuarter(Date date)
    {

        String formatDate = formatDate(date);
        Date parseDate = parseDate(formatDate);
        int month = getMonth(parseDate);
        int quarter=0;
        if (month<4){
            quarter=1;
        }else if (month<7){
            quarter=2;
        }else if (month<10){
            quarter=3;
        }else if (month<13) {
            quarter=4;
        }
        return quarter;
    }



	/**
	 * 取日期
	 * @param date
	 * @return
	 */
	public static int getDate(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DATE);
	}
	
	
	/**
	 * 转日期对象
	 * @param source
	 * @return
	 */
	public static Date parseDate(String source)
	{
		return parse(source, DATE_FORMAT);
	}

	/**
	 * 转日期对象
	 * @param source
	 * @return
	 */
	public static Date parseDateShort(String source)
	{
		if (StringUtils.isEmpty(source)) {
			return null;
		}
		return parse(source, DATE_FORMAT_SHORT);
	}
	
	/**
	 * 转日期时间对象
	 * @param source
	 * @return
	 */
	public static Date parseDateTime(String source)
	{
		return parse(source, DATE_TIME_FORMAT);
	}
	
	
	/**
	 * 转日期对象
	 * @param source
	 * @param format
	 * @return
	 */
	public static Date parse(String source, String format)
	{
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try
		{
			date = sdf.parse(source);
		} catch(Exception ex)
		{}
		return date;
	}
	
	/**
	 * 转日期格式字符串
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date)
	{
		return format(date, DATE_FORMAT);
	}
	
	/**
	 * 转日期格式字符串
	 * @param date
	 * @return
	 */
	public static String formatDateShort(Date date)
	{
		return format(date, DATE_FORMAT_SHORT);
	}
	
	
	/**
	 * 转日期格式字符串
	 * @param timeInMillions
	 * @return
	 */
	public static String formatDate(long timeInMillions)
	{
		return format(timeInMillions, DATE_FORMAT);
	}
	
	/**
	 * 转日期格式字符串
	 * @param timeInMillions
	 * @return
	 */
	public static String formatDateShort(long timeInMillions)
	{
		return format(timeInMillions, DATE_FORMAT_SHORT);
	}
	
	/**
	 * 转日期时间格式字符串
	 * @param date
	 * @return
	 */
	public static String formatDateTime(Date date)
	{
		return format(date, DATE_TIME_FORMAT);
	}
	
	/**
	 * 转日期时间格式字符串
	 * @param date
	 * @return
	 */
	public static String formatDateTimeShort(Date date)
	{
		return format(date, DATE_TIME_FORMAT_SHORT);
	}
	
	/**
	 * 转日期时间格式字符串
	 * @param timeInMillions
	 * @return
	 */
	public static String formatDateTime(long timeInMillions)
	{
		return format(timeInMillions, DATE_TIME_FORMAT);
	}
	
	/**
	 * 转日期时间格式字符串
	 * @param timeInMillions
	 * @return
	 */
	public static String formatDateTimeShort(long timeInMillions)
	{
		return format(timeInMillions, DATE_TIME_FORMAT_SHORT);
	}
	
	
	/**
	 * 转时间格式字符串
	 * @param date
	 * @return
	 */
	public static String formatTime(Date date)
	{
		return format(date, TIME_FORMAT);
	}
	
	/**
	 * 转时间格式字符串
	 * @param date
	 * @return
	 */
	public static String formatTimeShort(Date date)
	{
		return format(date, TIME_FORMAT_SHORT);
	}
	
	
	/**
	 * 转时间格式字符串
	 * @param timeInMillions
	 * @return
	 */
	public static String formatTime(long timeInMillions)
	{
		return format(timeInMillions, TIME_FORMAT);
	}
	
	/**
	 * 转时间格式字符串
	 * @param timeInMillions
	 * @return
	 */
	public static String formatTimeShort(long timeInMillions)
	{
		return format(timeInMillions, TIME_FORMAT_SHORT);
	}
	
	
	/**
	 * 当前日期转字符串
	 * @return
	 */
	public static String formatCurrentDate()
	{
		return format(new Date(), DATE_FORMAT);
	}
	
	/**
	 * 当前日期时间转字符串
	 * @return
	 */
	public static String formatCurrentDateTime()
	{
		return format(new Date(), DATE_TIME_FORMAT);
	}
	
	/**
	 * 当前日期时间转字符串
	 * @return
	 */
	public static String formatCurrentDateTimeShort()
	{
		return format(new Date(), DATE_TIME_FORMAT_SHORT);
	}
	
	
	/**
	 * 转字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String format(Date date, String format)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try
		{
			return sdf.format(date);
		} catch(Exception ex)
		{}
		return null;
	}
	
	
	/**
	 * 转字符串
	 * @param timeInMillions
	 * @param format
	 * @return
	 */
	public static String format(long timeInMillions, String format)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try
		{
			Date date = new Date(timeInMillions);
			return sdf.format(date);
		} catch(Exception ex)
		{}
		return null;
	}
	
	
	/**
	 * 转 java.sql.Date
	 * @param date
	 * @return
	 */
	public static java.sql.Date toSqlDate(Date date)
	{
		return new java.sql.Date(date.getTime());
	}
	
	/**
	 * 转 java.util.Date
	 * @param date
	 * @return
	 */
	public static Date toDate(java.sql.Date date)
	{
		return new Date(date.getTime());
	}

	/**
	 * 取当前年份
	 * @param date
	 * @return
	 */
	public static int getCurrentYear()
	{
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR);
	}
	/**
	 * 取当前月份
	 * @param date
	 * @return
	 */
	public static int getCurrentMonth()
	{
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.MONTH)+1;
	}

	/**
	 * 取当前日
	 * @param date
	 * @return
	 */
	public static int getCurrentDay()
	{
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.DAY_OF_MONTH);
	}


	/**
	 * 返回清空时间后的日历对象
	 * @return
	 */
	public static Calendar getDateCalendar()
	{
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c;
	}
	
	/**
	 * 
	 * @param src
	 * @param workdays
	 * @return
	 */
	public static Calendar addDays(Calendar src, int days)
	{
		src.add(Calendar.DAY_OF_MONTH, days);
		return src;
	}
	
	
	/**
	 * 
	 * @param src
	 * @param workdays
	 * @return
	 */
	public static Calendar addWorkDays(Calendar src, int workdays)
	{
		int days = 0;
		int step = 1;
		if(workdays < 0) step = -1;
		workdays = Math.abs(workdays);
		while(days < workdays)
		{
			src.add(Calendar.DAY_OF_MONTH, step);
			if(!isWorkDay(src)) continue;
			days ++;
		}
		return src;
	}
	
	/**
	 * 判断是否为工作日
	 * @param c
	 * @return
	 */
	public static boolean isWorkDay(Calendar c)
	{
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		/*周六日被认为不是工作日*/
		if(dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) return false;
		return true;
	}
	
	
}
